package com.zyw.dev.zyw.ui.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.app.LoaderManager.LoaderCallbacks;

import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;

import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.zyw.dev.zyw.R;
import com.zyw.dev.zyw.db.DataBaseHealper;
import com.zyw.dev.zyw.utils.SPUtils;

import static android.Manifest.permission.READ_CONTACTS;

/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends AppCompatActivity {

    public static final String LOGIN_CACHE_KEY = "login_cache_key";

    private String[][] userAccounts = {
            {"Admin", "111111"},
            {"Crew1", "123123"},
            {"Crew2", "234234"},
            {"Crew3", "345345"},
            {"Crew4", "456456"},
            {"Crew5", "567567"},
            {"Crew6", "678678"},
            {"Crew7", "789789"},
            {"Crew8", "888888"},
            {"Crew9", "999999"},
    };
    private EditText et_user_name;

    private EditText et_password;

    private Button btn_login;

    private DataBaseHealper dataBaseHealper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        // Set up the login form.
        initData();
        setUpView();
    }

    private void initData() {

    }

    private void setUpView() {
        btn_login = (Button) findViewById(R.id.btn_login);
        et_user_name = (EditText) findViewById(R.id.et_user_name);
        et_password = (EditText) findViewById(R.id.et_password);
        et_password.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                Log.e("TAG", "login");
                login();
                return false;
            }
        });

        btn_login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
    }

    // 登录
    private void login() {
        String name = et_user_name.getText().toString();
        String password = et_password.getText().toString();

        if (null == name || name.length() <= 0) {
            T(R.string.user_name_isnull);
            return;
        }

//        if (!name.equals("admin")) {
//            T(R.string.user_name_error);
//            return;
//        }

        if (null == password || password.length() <= 0) {
            T(R.string.password_isnull);
            return;
        }

//        if (!password.equals("admin")) {
//            T(R.string.password_error);
//            return;
//        }

        boolean isExist = false;
        for (String[] account : userAccounts) {
            if (account[0].equals(name) && (account[1].equals(password))) {
                isExist = true;
            }
        }

        if (!isExist){
            T(R.string.name_or_password_error);
            return;
        }

        SPUtils.put(this,LOGIN_CACHE_KEY,name+"@"+new Date().getTime());

        T(R.string.login_success);
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("isFromLogin",true);
        intent.putExtra("name",name);
        startActivity(intent);
        this.finish();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            System.exit(0);
        }
        return super.onKeyDown(keyCode, event);
    }

    private void T(int strId) {
        Toast.makeText(this, strId, Toast.LENGTH_SHORT).show();
    }
}

